import Instance from "../../../Core/Utils/Instance";
import { RandomUtils } from "../../../Core/Utils/RandomUtils";
import ConfigManager from "../../Config/ConfigManager";
import { item, lbConfig } from "../../Config/schema/schema";
import * as fgui from "fairygui-cc";
import { RewardVo } from "./RewardVo";
import UICore from "../../../Core/Res/UICore";
import { protoCmd } from "../../proto/protoCmd";
import { tween, Tween } from "cc";

export default class CoreUtil {
    private static tweenDict: Object = {};
    public static get ins(): CoreUtil {
        return Instance.get(CoreUtil);
    }
    /**
   * 是否有资源
   */
    public hasPackage(name: string): boolean {
        return fgui.UIPackage.getByName(name) != null;
    }

    /**
     * 是否有资源列表
     */
    public hasPackageList(list: Array<string>): boolean {
        for (var name of list) {
            if (!this.hasPackage(name)) {
                return false;
            }
        }
        return true;
    }
    /**
     * 数字滚动效果
     * @author konhg
     * @param text 显示滚动的组件实体
     * @param callback 回调函数
     * @param count 目标值
     * @param time 滚动的时间
     **/
    public static digitalScrolling(text: fgui.GTextField, callback: Function, count: number, time: number = 1500): void {
        if (!text) { return }
        if (this.tweenDict[text.id]) {
            if (Number(this.tweenDict[text.id]) == count) {
                return;
            }
        }
        if (Number(text.text) == count) {
            return;
        }
        if (text.text == '0') {
            text.text = count + '';
            return;
        }
        Tween.stopAllByTarget(text);
        this.tweenDict[text.id] = count;
        text.data = Number(text.text);
        let twen = tween(text).to(time / 1000, { data: count }, {
            onUpdate: (target: fgui.GTextField) => {
                if (!target || !target.data) { return }
                target.text = (Math.floor(target.data)) + '';
            }
        }).call(() => {
            text.text = count + '';
            Tween.stopAllByTarget(text);
            twen = null;
            delete this.tweenDict[text.id];
            if (callback) {
                callback();
            }
        }).start();
    }
}